<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/2 0002
 * Time: 13:56
 */
namespace app\app_manager\model\search;

use app\common\model\AppModel;

class AppSearch extends AppModel{

    public function search($params) {
        $limit = isset($params['limit'])?$params['limit']:0;
        $offset = isset($params['offset'])?$params['offset']:0;
        $sortName = isset($params['sortName'])?$params['sortName']:'id';
        $sortOrder = isset($params['sortOrder'])?$params['sortOrder']:'DESC';
        $keyword = isset($params['keywords'])?$params['keywords']:'';

        if ($keyword) {
            $count = $this->where('name|plat_secret|plat_key|plat_id', 'like', '%' . $keyword . '%')->count();
            $list = $this
                ->where('name|plat_secret|plat_key|plat_id', 'like', '%' . $keyword . '%')
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $this->field('id')->count(); //查询满足要求的总记录数
            $list = $this
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }
}